package service.impl;

import common.impl.exception.ServiceException;

import model.MScore;
import net.sf.json.JSONObject;
import service.interf.IScore;
import dao.interf.IScoreDao;
import dao.interf.IStudentDao;
import dao.interf.ITestDao;
import dao.pojo.Score;
import dao.pojo.Student;
import dao.pojo.Test;

public class ScoreImpl implements IScore {
	private IScoreDao scoreDao;
	private IStudentDao studentDao;
	private ITestDao testDao;

	public void setscoreDao(IScoreDao scoreDao) {
		this.scoreDao = scoreDao;
	}

	public void setStudentDao(IStudentDao studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public String add(MScore model) {
		Score score = new Score();
		if (null != model.getScoreNo())
			score.setScoreNo(model.getScoreNo());

		if (null != model.getStuId()) {
			Student stu = studentDao.getById(model.getStuId());
			if (stu == null) {
				throw new ServiceException("ERR-0011");
			} else {
				score.setStudent(stu);
			}
		}

		if (null != model.getTestId()) {
			Test test = testDao.getById(model.getTestId());
			if (test == null) {
				throw new ServiceException("ERR-0051");
			} else {
				score.setTest(test);
			}
		}

		score.setScoreListenAns(model.getScoreListenAns());
		score.setScoreReadAns(model.getScoreReadAns());
		score.setScoreSeletionAns(model.getScoreSeletionAns());
		score.setScoreWriteAns(model.getScoreWriteAns());
		score.setScoreTranslateAns(model.getScoreTranslateAns());
		score.setScoreRewriteAns(model.getScoreRewriteAns());

		return scoreDao.add(score);
	}

	@Override
	public String getById(MScore model) {
		Score score = scoreDao.getById(model.getScoreId());
		if (null == score) {
			throw new ServiceException("ERR-0131");
		}

		return createScoreObj(score).toString();
	}

	@Override
	public String deleteById(Integer id) {
		scoreDao.deleteById(id);
		return "1";
	}

	@Override
	public String edit(MScore model) {
		Score score = scoreDao.getById(model.getScoreId());
		if (null == score) {
			throw new ServiceException("ERR-0131");
		}

		if (score.getStudent().getStuId() != model.getStuId()) {
			Student stu = studentDao.getById(model.getStuId());
			if (stu == null) {
				throw new ServiceException("ERR-0011");
			} else {
				score.setStudent(stu);
			}
		}

		if (score.getTest().getTestId() != model.getTestId()) {
			Test test = testDao.getById(model.getTestId());
			if (test == null) {
				throw new ServiceException("ERR-0051");
			} else {
				score.setTest(test);
			}
		}

		score.setScoreNo(model.getScoreNo());
		score.setScoreListenAns(model.getScoreListenAns());
		score.setScoreReadAns(model.getScoreReadAns());
		score.setScoreSeletionAns(model.getScoreSeletionAns());
		score.setScoreWriteAns(model.getScoreWriteAns());
		score.setScoreTranslateAns(model.getScoreTranslateAns());
		score.setScoreRewriteAns(model.getScoreRewriteAns());

		scoreDao.edit(score);
		return "1";
	}

	private JSONObject createScoreObj(Score score) {
		JSONObject obj = new JSONObject();

		obj.put("scoreId", score.getScoreId());
		if (null != score.getStudent())
			obj.put("stuId", score.getStudent().getStuId());
		if (null != score.getTest())
			obj.put("testId", score.getTest().getTestId());
		obj.put("scoreNo", score.getScoreNo());
		obj.put("scoreListenAns", score.getScoreListenAns());
		obj.put("scoreReadAns", score.getScoreReadAns());
		obj.put("scoreSeletionAns", score.getScoreSeletionAns());
		obj.put("scoreWriteAns", score.getScoreWriteAns());
		obj.put("scoreTranslateAns", score.getScoreTranslateAns());
		obj.put("scoreRewriteAns", score.getScoreRewriteAns());

		return obj;
	}

}
